package kr.hkit.powerjava.chapter24;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

public class CopyFileStream {
	public static void main(String[] args) {
		
		BufferedInputStream fis = null; //try문에다가 바로 생성하면 안됨
		BufferedOutputStream fos = null; //buffered쓰면 속도 확연히 줄어듬
		long start, end;

		try {
			fis = new BufferedInputStream(new FileInputStream("Hydrangeas.jpg")); //파일 기본폴더는 JRE System Library
			fos = new BufferedOutputStream(new FileOutputStream("수국화.jpg",false)); //append가 ture라면 추가 
			 start = System.currentTimeMillis();
			
			int c;
			while ((c = fis.read()) != -1) {
//				System.out.printf("%s", (char) c);
				fos.write(c); // 복사~~
			}
			 
			end = System.currentTimeMillis();
		
			System.out.println("걸린 시간 " + (end-start));
			
			} catch (FileNotFoundException e) {
			System.err.format("%s%n", "파일이 존재하지 않음");
			} catch (IOException e) {
				e.printStackTrace();
			} finally {
			if (fis != null){
				try{fis.close();} catch(Exception e) {}
			}
			if (fos != null) {
				try{fos.close();} catch(Exception e) {}
			}
		}
	}
}
